package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : jiang
 * @date : 2023/10/20 8:53
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝数据
        BeanUtils.copyProperties(dto, shoppingCart);
        //补充数据
        //用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断用户添加的是菜品还是套餐
        if (dto.getDishId() != null) {
            //添加的是菜品
            Dish dish = dishMapper.gatById(dto.getDishId());
            //设置菜品名字
            shoppingCart.setName(dish.getName());
            //设置图片
            shoppingCart.setImage(dish.getImage());
            //设置价格
            shoppingCart.setAmount(dish.getPrice());
        } else {
            //添加的是套餐
            SetmealVO setmealVO = setmealService.getById(dto.getSetmealId());
            //设置套餐名称
            shoppingCart.setName(setmealVO.getName());
            //设置套餐图片
            shoppingCart.setImage(setmealVO.getImage());
            //设置套餐价格
            shoppingCart.setAmount(setmealVO.getPrice());
        }
        //补充number
        //判断用户是否第一次加入购物车
        ShoppingCart record = shoppingCartMapper.getRecord(shoppingCart);
        if (record != null) {
            //点过，数量加1
            shoppingCart.setNumber(record.getNumber() + 1);
            //记录id
            shoppingCart.setId(record.getId());
            //更新数据库数据
            shoppingCartMapper.updateRecord(shoppingCart);
        }else {
            //没点过 设置数量为1
            shoppingCart.setNumber(1);
            //补充创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加数据库记录
            shoppingCartMapper.addRecord(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //根据用户的id查询该用户的购物车信息
        List<ShoppingCart> cart = shoppingCartMapper.list(userId);
        //返回购物车数据
        return cart;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //获取当前操作用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteShoppingCart(userId);
    }

    /**
     * 删除购物车中的一个商品
     * @param dto
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        //创建购物车对象 拷贝数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //查询数据库
        ShoppingCart record = shoppingCartMapper.getRecord(shoppingCart);
        //设置要操作的记录id
        shoppingCart.setId(record.getId());
        //判断删除的商品数量
        if (record.getNumber() > 1){
            //修改记录的number
            shoppingCart.setNumber(record.getNumber() - 1);
            shoppingCartMapper.updateRecord(shoppingCart);
        }else {
            //删除记录
            shoppingCartMapper.sub(shoppingCart);
        }
    }
}
